package com.hzcx.app.simplechat.ui.chat;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.group.LiveData.GroupInfoBeanLiveData;
import com.hzcx.app.simplechat.ui.group.bean.GroupInfoBean;
import com.hzcx.app.simplechat.util.toast.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 群组公告设置
 */
public class ChatGroupNoticeActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.et_notice)
    EditText etNotice;
    @Override
    public int getLayout() {
        return R.layout.activity_chat_group_notice;
    }

    static public String typeTag = "ChatGroupNoticeActivity_TypeTag";
    static public String groupIDTag = "ChatGroupNoticeActivity_GroupIDTag";
    static public String valueTag = "ChatGroupNoticeActivity_ValueTag";
    private int tag;
    private String originStr = "";
    private String groupID = "";


    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {

        groupID = getIntent().getStringExtra(groupIDTag);
        tag = getIntent().getIntExtra(typeTag, 0);
        originStr = getIntent().getStringExtra(valueTag);

        if (tag == 0) {
            tvTitle.setText("群公告");
            etNotice.setHint("设置群公告");
        } else if (tag == 1) {
            tvTitle.setText("群介绍");
            etNotice.setHint("设置群简介");
        } else if (tag == 2) {
            tvTitle.setText("XXXX");
        }

        tvTitleRight.setText("完成");
        tvTitleRight.setTextColor(getResources().getColor(R.color.color_A6A8B0));

        etNotice.setText(originStr);
    }

    @OnClick({R.id.tv_title_right})
    public void viewOnClick(View view) {
       switch (view.getId()) {
           case R.id.tv_title_right:
               if (tag == 0) {
                   showGroupNotice();
               } else if (tag == 1) {
                   showGroupDesc();
               }
               break;
       }
    }

    /**
     * 群公告修改弹窗
     */
    private void showGroupNotice() {
        String newStr = etNotice.getText().toString();
        if (originStr.equals(newStr)) {
            ToastUtils.show("请先修改内容");
            return;
        }
        GroupModel.modifyGroupMotice(ChatGroupNoticeActivity.this, groupID, newStr, new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                GroupInfoBean groupInfoBean = GroupInfoBeanLiveData.getGroupInfoBean(groupID);
                groupInfoBean.setGroup_placard(newStr);
                finish();
            }
            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
                ToastUtils.show("修改失败");
            }
        });
    }

    /**
     * 群描述修改弹窗
     */
    private void showGroupDesc() {
        String newStr = etNotice.getText().toString();
        if (originStr.equals(newStr)) {
            ToastUtils.show("请先修改内容");
            return;
        }
        GroupModel.modifyGroupDescription(ChatGroupNoticeActivity.this, groupID, newStr, new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                GroupInfoBean groupInfoBean = GroupInfoBeanLiveData.getGroupInfoBean(groupID);
                groupInfoBean.setGroup_desc(newStr);
                finish();
            }
            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
                ToastUtils.show("修改失败");
            }
        });
    }


    @Override
    public void initData() {

    }

}
